import 'package:cher_flare/modules/login/controllers/ly_login_controller.dart';
import 'package:cher_flare/utils/app_color.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../http/http_config.dart';
import '../../../routes/app_pages.dart';
import '../../../utils/utils.dart';

class LYLoginAlertPage extends StatefulWidget {
  final bool isFastLogin;
  const LYLoginAlertPage({super.key, required this.isFastLogin});

  @override
  State<LYLoginAlertPage> createState() => _LYLoginAlertPageState();
}

class _LYLoginAlertPageState extends State<LYLoginAlertPage> {
  final controller = Get.find<LYLoginController>();
  final TapGestureRecognizer privacyPolicyGestureRecognizer =
      TapGestureRecognizer();
  final TapGestureRecognizer termOfUseGestureRecognizer =
      TapGestureRecognizer();

  @override
  void initState() {
    super.initState();
    privacyPolicyGestureRecognizer.onTap = () {
      Get.toNamed(AppPages.webView, arguments: {
        'urlString': HttpConfig.privacyPolicy,
        'title': 'Privacy Policy',
        'type': 0
      });
    };
    termOfUseGestureRecognizer.onTap = () {
      Get.toNamed(AppPages.webView, arguments: {
        'urlString': HttpConfig.termConditions,
        'title': 'Term of Use',
        'type': 1
      });
    };
  }

  void _agreeButtonDidClick() {
    if (widget.isFastLogin) {
      controller.login(true);
    } else {
      controller.appleLogin();
    }
  }

  void _cancelButtonDidClick() {
    Get.close(0);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: AppColors.clearColor,
      child: Center(
        child: Container(
          width: double.infinity,
          margin: const EdgeInsets.symmetric(horizontal: 30),
          padding: const EdgeInsets.only(top: 20, bottom: 24),
          decoration: BoxDecoration(
              gradient: const LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [AppColors.colorF4FDF1, AppColors.whiteColor],
              ),
              borderRadius: BorderRadius.circular(18)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Image.asset('assets/images/logo.png'),
              const Padding(
                padding: EdgeInsets.only(top: 8, bottom: 26),
                child: Text(
                  'CherFlare',
                  style: TextStyle(fontSize: 14, color: AppColors.color404040),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(left: 24, right: 24, bottom: 24),
                child: Text.rich(TextSpan(
                  children: [
                    const TextSpan(
                        text:
                            'By using our App you agree with our Term of Use and Privacy Policy.',
                        style: TextStyle(
                            fontSize: 12, color: AppColors.color202020)),
                    TextSpan(
                        recognizer: termOfUseGestureRecognizer,
                        text: 'Term of Use',
                        style: const TextStyle(
                            fontSize: 12,
                            color: Color.fromARGB(255, 169, 230, 83))),
                    const TextSpan(
                        text: ' and ',
                        style: TextStyle(
                            fontSize: 12, color: AppColors.color202020)),
                    TextSpan(
                        recognizer: privacyPolicyGestureRecognizer,
                        text: 'Privacy Policy.',
                        style: const TextStyle(
                            fontSize: 12,
                            color: Color.fromARGB(255, 169, 230, 83))),
                  ],
                )),
              ),
              _makeAgreeButton(),
              const SizedBox(
                height: 20,
              ),
              _makeCancelButton(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _makeAgreeButton() {
    return InkWell(
      onTap: () => Utils.throttle(_agreeButtonDidClick, continueClick: () {}),
      child: Container(
        height: 40,
        width: 180,
        margin: const EdgeInsets.symmetric(horizontal: 24),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20),
            gradient: const LinearGradient(
                colors: [AppColors.colorCCFF78, AppColors.color78FF85])),
        child: const Text(
          'Agree and Continue',
          style: TextStyle(color: AppColors.color404040, fontSize: 14),
        ),
      ),
    );
  }

  Widget _makeCancelButton() {
    return InkWell(
      onTap: () => _cancelButtonDidClick(),
      child: Container(
        height: 40,
        width: 180,
        margin: const EdgeInsets.symmetric(horizontal: 24),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          color: AppColors.colorF4f4f4,
        ),
        alignment: Alignment.center,
        child: const Text(
          'Cancel',
          style: TextStyle(color: AppColors.colorA9A9A9, fontSize: 14),
        ),
      ),
    );
  }
}
